﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;

namespace Qwen_for_Windows.Model
{
    public class Qwen_ChatHistory
    {
        private static string ChatHistoryPath = Path.Combine(Directory.GetCurrentDirectory(), "Data", "ChatHistory");
        public static void CreateChatHistoryFile()
        {
            // 1. 构建目录路径和文件名
          
            string fileName = $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.xml"; // 修正时间格式
            string filePath = Path.Combine(ChatHistoryPath, fileName);

            // 2. 确保目录存在
            if (!Directory.Exists(ChatHistoryPath))
            {
                Directory.CreateDirectory(ChatHistoryPath);
            }

            // 3. 构建XML内容
            string baseHistoryFile = $@"<?xml version=""1.0"" encoding=""UTF-8""?>
<ChatHistory>
    <Title>聊天记录 - {DateTime.Now:yyyy-MM-dd HH:mm:ss}</Title>
    <ModelVersion>{App_Model.Model_Name}</ModelVersion>
    <Messages>
    </Messages>
</ChatHistory>";

            // 4. 写入文件
            try
            {
                File.WriteAllText(filePath, baseHistoryFile, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                // 根据知识库建议，捕获异常并输出错误信息
                Console.WriteLine($"创建聊天记录文件失败: {ex.Message}");
                // 可在此处添加日志记录或用户提示
            }
        }
        public static void AddChatHistory_To_ListBox(ListBox listBox)
        {
            try
            {
                string[] files = Directory.GetFiles(ChatHistoryPath, "*.xml");
                foreach (string file in files)
                {
                    string fileName = Path.GetFileNameWithoutExtension(file);
                    string text=  Model.App_Model.GetChatFileTitle(file);                    
                    listBox.Items.Add(text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"获取聊天记录文件失败: {ex.Message}");
            }
        }

    }
}
